#include "HashTable.h"

HashTable::HashTable()
: m_hashTableSize(256)
, m_pHashTable(NULL)
{
	m_pHashTable = new HashNode[256];

	for(int i=0; i<m_hashTableSize; ++i)
	{
		m_pHashTable[i].key = -1;
		m_pHashTable[i].hash = -1;
	}
}
HashTable::~HashTable()
{
	//Check how to clean up properly
	
}
bool HashTable::AddNode(char* a_value)
{
	//Make sure a value was passed in
	if(a_value == NULL)
		return false;

	//Get the hashed key
	int hash = Hash(a_value);
	int key = hash;

	//If the spot in the array is already taken..
	while(m_pHashTable[key].key != -1)
	{
		//Keep searching for a blank node - wrap if necessary
		if(++key == m_hashTableSize)
			key = 0;
	}

	//No need to search through sub nodes, just create one
	HashNode newNode;
	newNode.key = key;
	newNode.hash = hash;
	m_pHashTable[key] = newNode;

	return true;
}
bool HashTable::RemoveNode(char* a_value)
{

	return false;
}
int HashTable::Find(char* a_value)
{
	//Make sure a value was passed in
	if(a_value == NULL)
		return false;

	//Get the hashed key
	int hash = Hash(a_value);
	int key = hash;

	//If the spot in the array is already taken..
	while(m_pHashTable[key].key != -1)
	{
		//if()
		//	break;
		//Keep searching for a blank node - wrap if necessary
		if(++key == m_hashTableSize)
			key = 0;
	}
	//Return minus 1, 0 is a valid hash
	return -1;
}
const unsigned int HashTable::Hash(const char* a_pKey)
{
	int sum = 0;
	int i = 0;
	while(a_pKey[i])
	{
		sum += a_pKey[i++];
	}
	return sum % 256;
}
const unsigned int HashTable::SubHash(const char* a_pKey)
{
	int sum = 0;
	int i = 0;
	while(a_pKey[i])
	{
		sum += a_pKey[i+=2];
	}
	return sum % 256;
}